package com.credit.management.system.web.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Syauthority;
import com.credit.management.system.service.ISyauthorityService;


@Controller
@RequestMapping(value="/syauthority")
public class SyauthorityController {
	@Autowired
	private ISyauthorityService syauthorityService;
	
	/**
	 * 进入syauthority.jsp页面
	 * @return
	 */
	@RequestMapping(value="/syauthority",method=RequestMethod.GET)
	public String syauthority(){
		//System.out.println("syauthority------controller---------");
		return "syauthority/syauthority";
	}
	/**
	 * syauthority.jsp返回所有的权限树
	 * @return
	 */
	@RequestMapping(value="/syauthorityTree",method=RequestMethod.POST)
	@ResponseBody
	public  List<Map<String,Object>> syauthorityTree(){
		 List<Map<String,Object>> maps=syauthorityService.findAllSyauthoritys();
		 return maps;
	}
	/**
	 * 插入子节点
	 * @param syauthority
	 * @return
	 */
	@RequestMapping(value="/insertChildNode",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertChildNode(Syauthority syauthority){
		Integer result=0;
		Syauthority syauthority1=syauthorityService.selectByPrimaryKey(Integer.parseInt(syauthority.getPid()));
		//如果父节点的级别比等于三就不能创建子节点
		if(syauthority1.getGrand()<=2){
			syauthority.setGrand(syauthority1.getGrand()+1);//子节点的级别是父节点加1
			result=syauthorityService.insertSelective(syauthority);
			System.out.println("syauthority"+syauthority);
			if(syauthority.getId()!=null){
				result=syauthority.getId();
			}
		}else{
			result=-1;//当为-1时说明该节点等级是3，不能创建子节点
		}
		return result;
	}
	/**
	 * 移除权限
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	@ResponseBody
	public Integer remove(Integer id){
		
		Integer result=	syauthorityService.deleteByPrimaryKey(id);
		return result;
	}
	/**
	 * 添加根目录，根目录，添加id(主键自增)，pid(不能和现有id相同，否则会是该节点的子节点),
	 * 为避免添加的pid和已添加的其他节点的id相同，根目录的添加，pid和它的id相等，就避免了这个问题，
	 * 思路：通过添加name,再根据返回的主键id修改pid为id即可
	 * @param syauthority
	 * @return
	 */
	@RequestMapping(value="/insertRoot",method=RequestMethod.POST)
	@ResponseBody
	public Integer insertRoot(Syauthority syauthority){
		
		Integer result=	syauthorityService.insertSelective(syauthority);
		if(syauthority.getId()!=null){
			
			syauthority.setPid(syauthority.getId().toString());
			syauthorityService.updateByPrimaryKeySelective(syauthority);
			result=syauthority.getId();
		}
		return result;
	}
	/**
	 * 修改权限名
	 * @param syauthority
	 * @return
	 */
	@RequestMapping(value="/updateName",method=RequestMethod.POST)
	@ResponseBody
	public Integer updateName(Syauthority syauthority){
		Integer result=syauthorityService.updateByPrimaryKeySelective(syauthority);
		return result;
	}
}
